package com.highspeed.bdk.gateway.socket.analyze.preset.deploy.request;

import com.highspeed.bdk.gateway.socket.analyze.preset.deploy.request.issued.base.DisplayType;
import com.highspeed.bdk.gateway.socket.analyze.preset.deploy.request.issued.base.InstructionType;
import com.highspeed.bdk.gateway.socket.analyze.preset.deploy.request.issued.instruction.AppDisplayInstruction;
import com.highspeed.bdk.gateway.socket.analyze.preset.deploy.request.issued.instruction.AppInstruction;
import com.highspeed.bdk.gateway.socket.analyze.preset.deploy.request.issued.instruction.LogicalLinkStatusDisplayInstruction;

import java.io.*;
import java.net.Socket;

public class Client {
    private Socket socket;

    public Client() {

    }

    private Client(String host, int port) {
        try {
            this.socket = new Socket(host, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMsg(InstructionType instructionType, short srcLogicalId, short destLogicalId, String version) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(instructionType.getMessageId() + "_" + instructionType.getDesc() + "_源逻辑Id_" + srcLogicalId + "_目的逻辑Id_" + destLogicalId + "版本_" + version + ".dat");
            OutputStream os = socket.getOutputStream();
            AppInstruction appInstruction = new AppInstruction();
            byte[] msg = appInstruction.createIssuedInstruction(instructionType, new byte[0], srcLogicalId, destLogicalId, version);
            os.write(msg);
            os.flush();
            socket.shutdownOutput();
            InputStream is = socket.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            int len = -1;
            byte[] response = new byte[1024 * 10];
            while ((len = bis.read(response)) != -1) {
                fileOutputStream.write(response, 0, len);
            }
            fileOutputStream.close();
            socket.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMsg(short srcLogicalId, short destLogicalId, short markAddr, DisplayType displayType) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("链路调显_" + displayType.getType() + "_" + displayType.getDesc() + "_源逻辑Id_" + srcLogicalId + "_目的逻辑Id_" + destLogicalId + "MARK物理地址_" + markAddr + ".dat");
            OutputStream os = socket.getOutputStream();
            AppDisplayInstruction appDisplayInstruction = new AppDisplayInstruction();
            byte[] msg = appDisplayInstruction.createdInstruction(srcLogicalId, destLogicalId, markAddr, displayType);
            os.write(msg);
            os.flush();
            socket.shutdownOutput();
            InputStream is = socket.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            int len = -1;
            byte[] response = new byte[1024 * 10];
            while ((len = bis.read(response)) != -1) {
                fileOutputStream.write(response, 0, len);
            }
            fileOutputStream.close();
            socket.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMsg(short srcAddr, byte frameType, short messageId, int length, byte requestType) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("逻辑链路状态调显_requestType" + requestType + ".dat");
            OutputStream os = socket.getOutputStream();
            LogicalLinkStatusDisplayInstruction l = new LogicalLinkStatusDisplayInstruction();
            byte[] msg = l.createInstruction(srcAddr, frameType, messageId, length, requestType);
            os.write(msg);
            os.flush();
            socket.shutdownOutput();
            InputStream is = socket.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            int len = -1;
            byte[] response = new byte[1024 * 10];
            while ((len = bis.read(response)) != -1) {
                fileOutputStream.write(response, 0, len);
            }
            fileOutputStream.close();
            socket.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String host = "192.168.0.185";
        int port = 8002;
        Client client = new Client(host, port);
        client.sendMsg((short) 0, (short) 0, (short) 0, DisplayType.FUNC);
    }

}
